﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.Linq;

namespace Common.DataTypes
{
    [XmlRoot("ItemSearchResponse", Namespace = "http://webservices.amazon.com/AWSECommerceService/2005-10-05")]
    public class AmazonBookCollection
    {
        [XmlArray("Items"), XmlArrayItem("Item", typeof(AmazonBook))]
        public List<AmazonBook> BookList { get; set; }

        public List<Book> ObtenerBooks()
        {
            List<Book> ListResult = new List<Book>();
            foreach (AmazonBook abook in BookList) {
                ListResult.Add(ConvertAmazonBookToBook(abook));
            }

            return (ListResult);
        }

        private Book ConvertAmazonBookToBook(AmazonBook amazonBook) {
            Book bookToConvert = new Book();

            #region Load Book Data
            bookToConvert.Asin = amazonBook.Asin;
            bookToConvert.Author = amazonBook.Attributes.Author;
            bookToConvert.Binding = amazonBook.Attributes.Binding;
            bookToConvert.DetailPageUrl = amazonBook.DetailPageUrl;
            bookToConvert.Ean = amazonBook.Attributes.Ean;
            bookToConvert.Isbn = amazonBook.Attributes.Isbn;
            bookToConvert.Label = amazonBook.Attributes.Label;
            if (amazonBook.LargeImage != null)
            {
                bookToConvert.LargeImage = amazonBook.LargeImage.Url;
            }
            // Load List Price
            ListPrice listPrice = amazonBook.Attributes.ListPrice;
            if (listPrice != null)
            {
                ListPriceData ListPriceData = new ListPriceData(int.Parse(listPrice.Amount), listPrice.FormattedPrice, listPrice.CurrencyCode);
                bookToConvert.ListPrice = ListPriceData;
            }
            bookToConvert.Manufacturer = amazonBook.Attributes.Manufacturer;
            if (amazonBook.MediumImage != null)
            {
                bookToConvert.MediumImage = amazonBook.MediumImage.Url;
            }
            bookToConvert.PublicationDate = amazonBook.Attributes.PublicationDate;
            bookToConvert.Publisher = amazonBook.Attributes.Publisher;
            bookToConvert.ReleaseDate = amazonBook.Attributes.ReleaseDate;
            bookToConvert.SalesRank = amazonBook.SalesRank;
            if (amazonBook.SmallImage != null)
            {
                bookToConvert.SmallImage = amazonBook.SmallImage.Url;
            }
            bookToConvert.Studio = amazonBook.Attributes.Studio;
            bookToConvert.Title = amazonBook.Attributes.Title;
            #endregion

            return (bookToConvert);
        }
    }
}
